package com.byx.chatserver.controller;

import com.byx.chatserver.dto.LoginDto;
import com.byx.chatserver.entity.*;
import com.byx.chatserver.service.INumberPoolService;
import com.byx.chatserver.service.IUserService;
import com.byx.chatserver.service.IVerificationCodeService;

import com.byx.springboot.unifiedresponseresult.entity.ResponseResult;
import com.byx.chatserver.vo.LoginVo;
import com.byx.springboot.unifiedresponseresult.entity.ServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * UserController
 *
 * @author Superb
 * @date 2021/2/7 21:20
 */
@ResponseResult
@RestController
@RequestMapping("/user")
public class UserController {

    private IUserService userService;
    @Autowired
    public void setUserService(@Qualifier("userServiceImpl") IUserService userService) {
        this.userService = userService;
    }

    private IVerificationCodeService verificationCodeService;
    @Autowired
    public void setVerificationCodeService(@Qualifier("emailVerificationCodeServiceImpl") IVerificationCodeService verificationCodeService) {
        this.verificationCodeService = verificationCodeService;
    }

    private INumberPoolService numberPoolService;
    @Autowired
    public void setNumberPoolService(@Qualifier("numberPoolServiceImpl") INumberPoolService numberPoolService) {
        this.numberPoolService = numberPoolService;
    }

    /*
     * 方法
     */

    @PostMapping("/login")
    public LoginDto login(@RequestBody LoginVo loginVo){
        return userService.loginByVo(loginVo);
    }

    @PostMapping("/register")
    public String register(@RequestParam("number") String number,
                         @RequestParam("password") String password,
                         @RequestParam("timestamp") Long timestamp,
                         @RequestParam("registerWay") String registerWay){
        int way = Integer.parseInt(registerWay);
        switch (way){
            case RegisterWay.BY_TEL:
                return userService.registerByTel(number,password,numberPoolService);

            case RegisterWay.BY_EMAIL:
                return userService.registerByEmail(number,password,timestamp,numberPoolService);

            default:
                throw new ServerException(ResultCode.PARAM_TYPE_BIND_ERROR);
        }
    }

    @PostMapping("/getVerificationCodeForRegister")
    public Boolean sendVerificationCodeForRegister(@RequestParam("number")String number,
                                       @RequestParam("type")String type){
        return sendCommonVerificationCode(number,type,MailType.REGISTER);
    }

    @PostMapping("/getVerificationCodeForLogin")
    public Boolean sendVerificationCodeForLogin(@RequestParam("number")String number,
                                                   @RequestParam("type")String type){
        return sendCommonVerificationCode(number,type,MailType.LOGIN);
    }

    @PostMapping("/getVerificationCodeForChangePwd")
    public String sendVerificationCodeForChangePwd(@RequestParam("number")String number,
                                                   @RequestParam("type")String type){
        //number为登录账号
        String email = userService.queryEmailByLoginId(number);
        if (sendCommonVerificationCode(email,type,MailType.CHANGE_PASSWORD)){
            return email;
        }
        throw new ServerException(ResultCode.VERIFICATION_CODE_SEND_FAILED);
    }

    private Boolean sendCommonVerificationCode(String number,String type,MailType mailType){
        int way = Integer.parseInt(type);
        switch (way){
            case RegisterWay.BY_EMAIL:
                return verificationCodeService.sendVerificationCode(number, mailType);
            case RegisterWay.BY_TEL:
            default:
                return Boolean.FALSE;
        }
    }

    @PostMapping("/checkVerificationCode")
    public Boolean checkVerificationCode(@RequestParam("number") String number,@RequestParam("code") String code){
        return verificationCodeService.isCorrectCode(number, code);
    }

    @PostMapping("/changePassword")
    public Boolean changePassword(@RequestParam("email") String email,@RequestParam("password") String pwd){
        return userService.changePasswordByEmail(email,pwd);
    }

    @RequestMapping("/index")
    public String home(){
        return "index";
    }

}
